<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { InvitationsTemplateVO } from '#/api/config/invitations/model';

import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { Image, Modal, Popconfirm, Space, Spin } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import {
  invitationsTemplatePage,
  invitationsTemplateRemove,
} from '#/api/config/invitations';
import fallbackImageBase64 from '#/views/system/oss/fallback-image.txt?raw';

import downloadModal from './components/download-modal.vue';
import { columns, querySchema } from './data';
import invitationsDrawer from './invitations-drawer.vue';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await invitationsTemplatePage({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  showOverflow: false,
  // 表格全局唯一表示 保存列配置需要用到
  id: 'invitations-template-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [InvitationsDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: invitationsDrawer,
});

function handleAdd() {
  drawerApi.setData({});
  drawerApi.open();
}

async function handleEdit(row: InvitationsTemplateVO) {
  drawerApi.setData({ id: row.id });
  drawerApi.open();
}

// 下载图片
const [DownloadModal, downloadModalApi] = useVbenModal({
  connectedComponent: downloadModal,
});

async function handleDownload(row: InvitationsTemplateVO) {
  downloadModalApi.setData({ imgUrl: row.imgUrl });
  downloadModalApi.open();
}

async function handleDelete(row: InvitationsTemplateVO) {
  await invitationsTemplateRemove(row.id);
  await tableApi.query();
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: InvitationsTemplateVO) => row.id);
  Modal.confirm({
    title: '提示',
    okType: 'danger',
    content: `确认删除选中的${ids.length}条记录吗？`,
    onOk: async () => {
      await invitationsTemplateRemove(ids);
      await tableApi.query();
    },
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable>
      <template #table-title>
        <main-title text="邀请函图片列表" />
      </template>
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            v-access:code="['invitation:template:remove']"
            @click="handleMultiDelete"
          >
            {{ $t('pages.common.delete') }}
          </a-button>
          <a-button
            type="primary"
            v-access:code="['invitation:template:add']"
            @click="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #imgOssId="{ row }">
        <Image
          v-if="row.imgOssId"
          :key="row.imgOssId"
          :src="row.imgUrl"
          height="100px"
          :fallback="fallbackImageBase64"
        >
          <template #placeholder>
            <div class="flex size-full items-center justify-center">
              <Spin />
            </div>
          </template>
        </Image>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['invitation:template:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <ghost-button @click.stop="handleDownload(row)">下载</ghost-button>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              v-access:code="['invitation:template:remove']"
              danger
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <InvitationsDrawer @reload="tableApi.query()" />
    <DownloadModal />
  </Page>
</template>
